﻿#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setAcceptDrops(true);
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls())
    {
        event->acceptProposedAction();
    }
}

void Widget::dropEvent(QDropEvent *event)
{
    const QMimeData *mimeData = event->mimeData();
    if (mimeData->hasUrls())
    {
        QList<QUrl> urlList = mimeData->urls();
        for (const QUrl &url : urlList)
        {
            QString filePath = url.toLocalFile();
            if(filePath.contains(".png", Qt::CaseInsensitive))
            {
                fixPNG(filePath);
                qDebug() << filePath << QString::fromUtf16(u" ---已优化");
            }
            else
            {
                qDebug() << filePath << QString::fromUtf16(u" ###非PNG文件");
            }
        }
    }
}

void Widget::fixPNG(QString pngPath)
{
    QImage img(pngPath);
    img.save(pngPath);
}

